﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using ClasesLibreria.dominio;

namespace ClasesLibreria.dao
{
    public static class DAOTipoDocumento
    {
        public static SqlDataReader queryTiposDocumentos(string connectionString)
        {
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            conn = new SqlConnection();
            conn.ConnectionString = connectionString;
            conn.Open();

            cmd = new SqlCommand("SELECT idTipoDocumento, nombre FROM tipodocumento ORDER BY idTipoDocumento");
            cmd.Connection = conn;

            dr = cmd.ExecuteReader();

            return dr;

        }

        public static TipoDocumento getById(int id, string connectionString)
        {
            TipoDocumento tipoDoc = new TipoDocumento();
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            conn = new SqlConnection();
            conn.ConnectionString = connectionString;
            conn.Open();
            cmd = new SqlCommand("SELECT IdtipoDocumento, Nombre FROM TipoDocumento WHERE IdtipoDocumento = @IdtipoDocumento");
            cmd.Connection = conn;
            cmd.Parameters.Add(new SqlParameter("@IdtipoDocumento", id));
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                tipoDoc.Id = int.Parse(dr["IdtipoDocumento"].ToString());
                tipoDoc.Nombre = dr["Nombre"].ToString();
                break;
            }
            conn.Close();
            return tipoDoc;
        }
    }
}
